import { QueryClient, useMutation } from "@tanstack/react-query";
import { useForm } from "react-hook-form";
import { userControllerSetRole } from "@/shared/api/api.user";
import { AxiosError } from "axios";

export function useSetRole() {
    const { register, handleSubmit } = useForm<{
        email: string;
        role: string;
    }>();
    const queryClient = new QueryClient

    const signInMutation = useMutation(
        {
            mutationFn: userControllerSetRole,
            onError: (error: (AxiosError<{ message: string }>)) => error,
        }, queryClient
    );
    const errorMessage = signInMutation.error ? signInMutation.error.response?.data.message : undefined;

    return {
        register,
        errorMessage,
        handleSubmit: handleSubmit((data) => signInMutation.mutate(data)),
    };
}